package logic

import (
	"context"
	"fmt"
	"order/models/mysql"

	"order/orderrpc/internal/svc"
	"order/orderrpc/orderrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserAddLogic {
	return &UserAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserAddLogic) UserAdd(in *orderrpc.UserAddRequest) (*orderrpc.UserAddResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	if user.ID == 0 {
		user = mysql.User{
			Account:  in.Account,
			Password: in.Password,
		}
		mysql.DB.Debug().Create(&user)
	} else {
		if user.Password != in.Password {
			return nil, fmt.Errorf("当前密码错误，请重修输入")
		}
	}
	return &orderrpc.UserAddResponse{
		UserId: int64(user.ID),
	}, nil
}
